const UserService = require("../../services/web/UserService")
// const JWT = require("../../util/JWT")
const svgCaptcha = require('svg-captcha'); //验证码模块
const UserController = {
    // 拿到前端的数据进行分析拆解 拿回来送给service进行处理 再返回
    login: async(req,res)=>{ 
        // console.log("111:",req.body)
        const captcha = svgCaptcha.create();

        var result = await UserService.login(req.body)
        if(result.length===0){
            res.send({
                code:"-1",
                error:"用户名密码不匹配",
                captchaData:{
                    text:captcha.text,
                    data:captcha.data,
                }
            })
        }else{
            //生成token
            // const token = JWT.generate({
            //     _id:result[0]._id,
            //     username:result[0].username
            // },"1d") // 设置时长 app.js里面有 "10s"   1d = 1 天
            // res.header("Authorization",token)
            res.send({
                ActionType:"OK",
                data:{
                    username:result[0].username,
                    propname:result[0].propname,
                    building:result[0].building,  
                    roomnumber:result[0].roomnumber,

                }
            })
        }
    },

    // upload:async(req,res)=>{
    //     const {username,introduction,gender} = req.body
    //     //调用servise模块更新数据
    //     const token = req.headers["authorization"].split(" ")[1]
    //     const avatar = req.file?`/avataruploads/${req.file.filename}`:""
    //     var payload = JWT.verify(token)

    //     await UserService.upload({_id:payload._id,username,introduction,
    //     gender:Number(gender),avatar})
    //     if(avatar){
    //         res.send({
    //             ActionType:"ok",
    //             data:{
    //                 username,introduction,
    //                 gender:Number(gender),
    //                 avatar
    //             }
    //         })
    //     }else{
    //         res.send({
    //             ActionType:"ok",
    //             data:{
    //                 username,introduction,
    //                 gender:Number(gender)
    //             }
    //         })
    //     }
    // },

    // add:async(req,res)=>{
    //     const {username,introduction,gender,role,password} = req.body
    //     //调用servise模块更新数据
    //     // const token = req.headers["authorization"].split(" ")[1]
    //     const avatar = req.file?`/avataruploads/${req.file.filename}`:""
    //     // var payload = JWT.verify(token)

    //     await UserService.add({username,introduction,
    //     gender:Number(gender),avatar,role:Number(role),password})
    //     if(avatar){
    //         res.send({
    //             ActionType:"ok",
                
    //         })
    //     }
    // },

    // getList:async(req,res)=>{
    //     const result = await UserService.getList(req.params)
    //     // console.log(result)
    //     res.send({
    //         ActionType:"ok",
    //         data:result
    //     })
    // }, 
    // putList:async(req,res)=>{
    //     const result = await UserService.putList(req.body)
    //     // console.log(result)
    //     res.send({
    //         ActionType:"ok",
    //     })
    // },   
    // delList:async(req,res)=>{
    //     // console.log(req.params.id)
    //     const result = await UserService.delList({_id:req.params.id})
    //     res.send({
    //         ActionType:"delok",
    //     })
    // },

}
module.exports = UserController
